import {create} from 'zustand';

export interface UserInfo {
    id: number;
    user: string;
    avatar?: string;

    [key: string]: any;
}

interface UserState {
    user: UserInfo | null;
    setUser: (user: UserInfo) => void;
    clearUser: () => void;
}

export const useUserStore = create<UserState>((set: any) => ({
    user: null,
    setUser: (user: any) => set({user}),
    clearUser: () => set({user: null}),
}));

// 后台管理页面/请求前校验示例
export function requireAdminLogin() {
    const user = useUserStore.getState().user;
    if (!user) {
        // 可自定义跳转或弹窗
        window.location.href = '/admin/login';
        throw new Error('请先登录后台');
    }
}
